package com.qiezzi.eggplant;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.widget.Toast;

import net.tsz.afinal.FinalDb;

import java.io.File;

/**
 * 作    者：高学军
 * 时    间：2015/9/6
 * 描    述：
 * 修改时间：
 */
public class Eggplant extends Application{
    //视频存放路径
    public static final String Eggplant_SD_CARD = "eggplant_cachekey_move";


    //下载路径
    public static File fBaseDir;
    //数据库
    public static FinalDb db;
    //上下文
    public static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        db = FinalDb.create(context, false);
        getBaseDir();
    }

    public static void getBaseDir() {
        // 下载到SD卡
        if (Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            fBaseDir = context
                    .getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
        } else {
            fBaseDir = new File(Eggplant.context.getFilesDir(),
                    Environment.DIRECTORY_DOWNLOADS);
        }
        if (fBaseDir != null) {
            if (!fBaseDir.exists() && !fBaseDir.mkdir()) {
                Toast.makeText(context, "缓存目录不能写", Toast.LENGTH_SHORT).show();
            }

            if (!fBaseDir.isDirectory()) {
                Toast.makeText(context, "缓存目录名已存在但不是目录", Toast.LENGTH_SHORT)
                        .show();
            }
        }

    }

    public static File getImgDir() {
        File dir = new File(fBaseDir, "img");
        if (!dir.exists()) {
            dir.mkdir();
        }
        return dir;
    }

    public static File getVideoDir() {
        File dir = new File(fBaseDir, "video");
        if (!dir.exists()) {
            dir.mkdir();
        }
        return dir;
    }

    //视频缓存的地址
    public  static File getDownVideoFile(String video){
        File ad_card_file = new File(getVideoDir(),
                Eggplant_SD_CARD + video);
        return ad_card_file;
    }

    //图片缓存地址
    public  static  File getDownImageFile(String image){
        File ad_card_file = new File(getImgDir(),
                Eggplant_SD_CARD + image);
        return ad_card_file;
    }
}
